package server.content.skills.thieving;

import core.util.Misc;
import server.content.skills.misc.SkillConstants;
import server.game.players.Client;

public class Theiving {

	private final Client c;

	public Theiving(final Client c) {
		this.c = c;
	}

	public void stealFromNpc(final int npcId) {
		if (System.currentTimeMillis() - getClient().lastThieve < 2000) {
			return;
		}
		for (final THEIVING_NPCS npc : THEIVING_NPCS.values()) {
			for (int i = 0; i < npc.getNpcIds().length; i++) {
				if (npc.getNpcIds()[i] == npcId) {
					if (getClient().playerLevel[SkillConstants.THIEVING] >= npc
							.getLvlReq()) {
						final int stun = Misc.random(10);
						if (stun <= npc.getChance()) {
							getClient().sendMessage(
									"You have been caught stealing...");
							getClient().stopMovement();
							getClient().gfx100(254);
							return;
						} else {
							getClient().sendMessage(
									"You manage to steal some "
											+ getClient().getItems()
													.getItemName(npc.getItem())
											+ ".");
							getClient().startAnimation(881, 0);
							getClient().getPA().requestUpdates();
							getClient().getItems().addItem(npc.getItem(),
									Misc.random(npc.getAmnt()));
							getClient().getPA().addSkillXP(npc.getXP(),
									SkillConstants.THIEVING);
							getClient().lastThieve = System.currentTimeMillis();
							return;
						}
					} else {
						getClient().sendMessage(
								"You need " + npc.getLvlReq()
										+ " thieving to steal from this NPC.");
						return;
					}
				}
			}
		}
	}

	public void stealFromStall(final int stallId) {
		if (System.currentTimeMillis() - getClient().lastThieve < 4000) {
			return;
		}
		for (final THIEVING_STALLS stall : THIEVING_STALLS.values()) {
			for (int i = 0; i < stall.getObjectIds().length; i++) {
				if (stall.getObjectIds()[i] == stallId) {
					if (getClient().playerLevel[SkillConstants.THIEVING] >= stall
							.getLvlReq()) {
						getClient().startAnimation(881, 0);
						getClient().getPA().requestUpdates();
						final int toAdd = Misc.random(stall.getItems().length);
						if (stall.getItems()[toAdd] > -1) {
							getClient().getItems().addItem(
									stall.getItems()[toAdd],
									stall.getAmnts()[toAdd]);
							getClient().sendMessage("You manage to steal some " + getClient().getItems().getItemName(stall.getItems()[toAdd]));
							getClient().lastThieve = System.currentTimeMillis();
							break;
						}
					} else {
						c.sendMessage("You need a thieving level of " + stall.getLvlReq() + " to steal from this stall.");
					}
				}
			}
		}
	}

	public Client getClient() {
		return c;
	}

}
